package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SearchhistoryMapper searchhistoryMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public ListBean queryUserListBean(String username,String mobile,Integer page,Integer limit,String sort,String order) {
        //分页
        PageHelper.startPage(page, limit);

        //example提供查询条件
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(username!=null){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(mobile!=null){
             criteria.andMobileEqualTo(mobile);
        }
//        userExample.createCriteria().andUsernameLike("%"+username+"%").andMobileEqualTo(mobile);
        userExample.setOrderByClause(sort+" "+order);
        List<User1> users = userMapper.selectByExample(userExample);

        PageInfo pageInfo = new PageInfo(users);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(users);
//        listBean.setTotal(users.size());
        listBean.setTotal(total);

        return listBean;
    }

    @Override
    public ListBean queryUserCollectListBean(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        collectExample.setOrderByClause(sort+" "+order);

        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo pageInfo = new PageInfo(collects);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(collects);
        listBean.setTotal(total);

        return listBean;
    }

    @Override
    public ListBean queryUserFootprintListBean(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page, limit);

        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(goodsId!=null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        footprintExample.setOrderByClause(sort+" "+order);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo pageInfo = new PageInfo(footprints);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(footprints);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public ListBean queryUserHistoryListBean(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        PageHelper.startPage(page, limit);

        SearchhistoryExample searchhistoryExample = new SearchhistoryExample();
       SearchhistoryExample.Criteria criteria = searchhistoryExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword!=null){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        searchhistoryExample.setOrderByClause(sort+" "+order);
        List<Searchhistory> searchhistories = searchhistoryMapper.selectByExample(searchhistoryExample);
        PageInfo pageInfo = new PageInfo(searchhistories);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(searchhistories);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public ListBean queryUserFeedbackListBean(Integer page, Integer limit, String username, String sort, String order, Integer id) {
        PageHelper.startPage(page, limit);

        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if(username!=null){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        feedbackExample.setOrderByClause(sort+" "+order);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo pageInfo = new PageInfo(feedbacks);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(feedbacks);
        listBean.setTotal(total);
        return listBean;
    }
}
